<?php
include_once("$config[webroot]/lib/Httpsqs.php");
	if($_GET['operation']=='getbrand'){
	    //根据品牌名搜索品牌
	    function get_brand_new($brandName,$limit=10){
	        global $db;
	        if($brandName==''){
	            $db->query("SELECT id,name FROM ".BRAND." ORDER BY create_time DESC LIMIT ".$limit);
	        }else{
	            $db->query("SELECT id,name FROM ".BRAND." WHERE name LIKE '%".mysql_real_escape_string($brandName)."%' ORDER BY create_time DESC");
	        }
	        $brandInfo = $db->getRows();
	        $op = "";
	        foreach($brandInfo as $v)
	        {
	            $op.='<li style="cursor:pointer;" bid="'.$v["id"].'">'.$v["name"].'</li>';
	        }
	        return $op;
	    }
	    echo get_brand_new($_GET['brandname']);
	    exit;
	}
	if($_GET['act']=="export_product_id"||$_GET['act']=='export_product'||$_GET['act']=='export_product_sku'){
		$act = $_GET['act'];
		$_GET = $_GET['param'];
		$_GET['act']=$act;unset($act);
	}
	if($_POST['act']=='op')
	{
	    
	    if(!in_array($_POST['type'], array(-9,-1,1,2,-3))){
	        exit('type -9,-1,1,2,-3 error');
	    }
	    
	    
		if(is_array($_POST['chk']))
		{
		    include_once("$config[webroot]/module/product/includes/plugin_pro_class.php");
		    $pro=new pro();
		    
			$id=implode(",",$_POST['chk']);
			$sql="update ".PRO." set status=".$_POST['type']." where id in ($id)";
			$db->query($sql);
			$sql="update ".SETMEAL." set status=".$_POST['type']." where pid in ($id) and `status`>-9";
			
			$db->query($sql);
			
			$idArr = explode(',', $id);
			foreach ($idArr as $pid)
			{
			    //更新单主商品单SKUID
			    $pro->update_single_sku_id($pid);
			    //更新solr
			    Httpsqs::put('solrProducts',array('product_id'=>$pid));
			}
			
			unset($_GET['m']);
			unset($_GET['s']);
			$param = "";
			if(count($_GET)){
				$param .= "&".http_build_query($_GET);
			}
			msg("?m=product&s=product.php".$param);
		}
	}
	$psql='';
	if($_GET['name'])
	{
		$psql.=" and a.pname like '%$_GET[name]%' ";	
	}
	if($_GET['code'])
	{
		$psql.=" and a.id in (".trim($_GET[code],',').")";	
	}
	if($_GET['statu']!==''&&isset($_GET['statu'])){
	    $psql.=" and a.status = '$_GET[statu]'";
	     
	}
	if($_GET['cat'])
	{
	    $psql.=" and a.catid like '$_GET[cat]%'";
	}
	if($_GET['brand'] && $_GET['brandid']){
	    $psql.=" and a.brandid = '$_GET[brandid]'";
	}else{
	    $psql.=" and a.brand like '$_GET[brand]%'";
	}
	if(!empty($_GET['tags']))
	{
	    $psql.=" and (a.tags like '$_GET[tags]%' or a.tags like '%,$_GET[tags]%') ";
	}
	if(!empty($_GET['stock_shop']))
	{
		$psql.=" and a.stock_shop='$_GET[stock_shop]'";
	}
	if($_GET['in_micro_shop']=='0'){
	    $psql.=" and a.ms_status=0";
	}
	if($_GET['in_micro_shop']=='1'){
	    $psql.=" and a.ms_status!=0";
	}
	
	if (!isset($_GET['admin_user'])){
	    $db->query(" select 1 from ".ADMIN." where group_id>0 and user='$_SESSION[ADMIN_USER]'");
	    if($db->num_rows()){
	        $admin_user = $_SESSION[ADMIN_USER];
	    }
	}elseif (!empty($_GET['admin_user'])){
	    $admin_user=$_GET['admin_user'];
	    $psql.=" and a.create_user = '$admin_user'";
	}
	
	if ($admin_user){
	    $tpl->assign('admin_user',$admin_user);
	}
	
	
	
	$export_sql=$sql="SELECT GROUP_CONCAT(b.code) as setmeal_code,
	GROUP_CONCAT(b.stock) as setmeal_stock,
	GROUP_CONCAT(b.id) as setmeal_skuid,
	GROUP_CONCAT(b.price) as setmeal_price,
	GROUP_CONCAT(b.market_price) as setmeal_market_price,
	GROUP_CONCAT(b.cost_price) as setmeal_cost_price,
	GROUP_CONCAT(b.spec) as setmeal_spec,

	count(b.id) as setmeal_nums,
	
	b.id,
	b.pid,
	a.pic,
	a.pname,
	a.brand,
	a.catid,
	a.tags,
	a.all_stock,
	a.code,
	a.waiting_time,
	a.waiting_time_end,
	a.stock_shop,
	b.uptime,
	FROM_UNIXTIME(b.uptime, '%Y-%m-%d %H:%i:%s' )as buptime,
	a.status,
    a.create_user,     
    a.create_time     
	FROM ".PRO." a left join ".SETMEAL." b on a.id = b.pid WHERE a.status>-9 and b.status>-9 $psql group by pid order by a.id desc";
	//===================
	include_once("../includes/page_utf_class.php");
	$page = new Page;
	$page->listRows=20;
	$db->query($sql);
	$totalRows = $db->num_rows();
	if (!$page->__get('totalRows')){
		$db->query($sql);
		$page->totalRows = $db->num_rows();
	}
	$sql .= "  limit ".$page->firstRow.",".$page->listRows;
	$de['page'] = $page->prompt();
	//=====================
	
	if($_GET['act']=="export_product_id"||$_GET['act']=='export_product'||$_GET['act']=='export_product_sku'){
	    include_once '../lib/php_excel.php';
	    $db->query($export_sql);
	    $export_data=$db->getRows();
	    if ($_GET['act']=='export_product'){
	    	//商品ID	skuid	SKUCODE	商品条码	1级分类	2级分类	3级分类	品牌	商品名称	商品短描述	销售属性	销售价	成本价	市场价	重量	备货期	实际库存	平均采购价格
	    
	    	$queue[0] = array("商品id","SKU总数","1级分类","2级分类","3级分类","商品名称","品牌","销售价","市场价","备货期","总库存","状态","作者","创建时间","最后修改时间");//定义表头
	    }elseif ($_GET['act']=='export_product_sku'){
	    	$queue[0] = array("总数", "商品id","商品","名称","品牌","价格","市场价","成本价","修改时间","标示","数量","类id","时间","状态","作者");//定义表头
	    		    
	    }else {
	    	$queue[0] = array("商品id");//定义表头
	    }
	    $arr = array();
	    
	    $status_array=array('-3'=>'已下架','-2'=>'已送审','-1'=>'被驳回','0'=>'未送审','1'=>'已上架','2'=>'推荐');
	    foreach ($export_data as $val)
	    {
	    	
	    	
	    	if ($_GET['act']=="export_product_id"){
	    		$arr[] = array($val['pid']);
	    	}
	    	elseif ($_GET['act']=='export_product'){
	    	    $catname = array();
	    	    
	    		if(strlen($val['catid'])>8)
	    			$catname[]=getName(substr($val['catid'],0,-6));
	    		if(strlen($val['catid'])>6)
	    			$catname[]=getName(substr($val['catid'],0,-4));
	    		if(strlen($val['catid'])>4)
	    			$catname[]=getName(substr($val['catid'],0,-2));
	    		
	    		$catname[]=getName($val['catid']);
	    		
	    		if($val['waiting_time'] && $val['waiting_time_end']){
	    		    $val['waiting_time_desc'] = $val['waiting_time']."-".$val['waiting_time_end']."天发货";
	    		}elseif($val['waiting_time']){
	    		    $val['waiting_time_desc'] = $val['waiting_time']."天发货";
	    		}elseif($val['waiting_time_end']){
	    		    $val['waiting_time_desc'] = $val['waiting_time_end']."天发货";
	    		}else{
	    		    $val['waiting_time_desc'] = "暂无";
	    		}
	    		
	    		$arr[] = array($val['pid'],$val['setmeal_nums'],$catname[0],$catname[1],$catname[2],$val['pname'],$val['brand'],$val['setmeal_price'],$val['setmeal_market_price'],$val['waiting_time_desc'],$val['all_stock'],$status_array[$val['status']],$val['create_user'],$val['create_time'],$val['buptime']);
	    	}
	    }
	    $export_data = array_merge($queue,$arr);
	    $filename =   "商品信息" . date ( "YmdHis" );
	    $xls = new Excel_XML ( 'UTF-8', false, '商品信息' );
	    $xls->addArray ( $export_data );
	    $xls->generateXML ( $filename );
	    exit ();
	}else{
	    $db->query($sql);
	    $de['list']=$db->getRows();
	}
	
	/***加入特卖中的产品**/
	$now_time = time();
	$act_pro_sql = "select hapl.product_id,hapl.activity_id,hapl.start_time,hapl.end_time,ha.title from ".ACTIVITYPRODUCT." hapl left join ".ACTIVITY." ha ON hapl.activity_id=ha.id where hapl.status>0";
	$db->query($act_pro_sql);
	$act_products = $db->getRows();
	$activity_products = array();
	foreach ($act_products as $apv)
	{
		$aStatus = 1;
		if($apv['start_time']>$now_time){
			$aStatus = 2;
		}
		if($apv['end_time']<$now_time){
			$aStatus = 3;
		}
		$activity_products[$apv['product_id']] = array('activity_id'=>$apv['activity_id'],'title'=>$apv['title'],'status'=>$aStatus);
	}
	/**加入特卖中的产品**/
	
	foreach($de['list'] as $key=>$val)
	{
		$catname=array();
		if(strlen($val['catid'])>8)
			$catname[]=getName(substr($val['catid'],0,-6));
		if(strlen($val['catid'])>6)
			$catname[]=getName(substr($val['catid'],0,-4));
		if(strlen($val['catid'])>4)
			$catname[]=getName(substr($val['catid'],0,-2));
		
		$catname[]=getName($val['catid']);
		
		$de['list'][$key]['catname']=implode('->',$catname);
		$de['list'][$key]['tags_arr'] = get_product_tags($val['tags']);

		if($val['waiting_time'] && $val['waiting_time_end']){
			$de['list'][$key]['waiting_time_desc'] = $val['waiting_time']."-".$val['waiting_time_end']."天发货";
		}elseif($val['waiting_time']){
			$de['list'][$key]['waiting_time_desc'] = $val['waiting_time']."天内发货";
		}elseif($val['waiting_time_end']){
			$de['list'][$key]['waiting_time_desc'] = $val['waiting_time_end']."天内发货";
		}else{
			$de['list'][$key]['waiting_time_desc'] = "暂无";
		}		
		
		//处理 多规格的情况 zx2014新增
		
		
		foreach (array('code','stock','price','market_price','cost_price','spec','skuid') as $item){
		    
		    $_tmp_flag= explode(',', $val['setmeal_'.$item]);
		    
		    $de['list'][$key]['setmeal_'.$item]=$_tmp_flag;
		    
		}
		
		//正在特卖的活动信息
		if(!empty($activity_products[$val['pid']])){
			$de['list'][$key]['actInfo'] = $activity_products[$val['pid']];
		}
		
	}
	
	
	
	//分类
	$sql="select * from ".PCAT." where catid<9999 order by displayorder,catid";
	$db->query($sql);
	$de['cat']=$db->getRows();
	foreach($de['cat'] as $key=>$val)
	{
	    $sql="select * from ".PCAT." where catid < '".$val['catid']."99' and catid > '".$val['catid']."00' order by displayorder,catid";
	    $db->query($sql);
	    $a=$db->getRows();
	    foreach($a as $ke=>$va)
	    {
	        $sql="select * from ".PCAT." where catid < '".$va['catid']."99' and catid > '".$va['catid']."00' order by displayorder,catid";
	        $db->query($sql);
	        $a[$ke]['scat']=$db->getRows();
	    }
	    $de['cat'][$key]['scat']=$a;
	}
	
	$sql=" select user,name from ".ADMIN." where group_id>57";
	$db->query($sql);
	$de['admins']=$db->getRows();
	
	
	
	unset($_GET['s']);
	unset($_GET['m']);
	unset($_GET['category_id']);
	unset($_GET['catid']);
	unset($_GET['tcatid']);
	unset($_GET['scatid']);
	unset($_GET['sscatid']);
	
	//所有标签
	include_once("$config[webroot]/module/product/includes/plugin_pro_class.php");
	//================================================================
	$pro=new pro();
	$de['all_tags']=$pro->get_tags();
	if ($_GET['tags']){
	    $de['tags']=array($_GET['tags']);
	}
	
	$de["stock_shops"]=$pro->get_stock_shops();
	
	if (count($_GET)){
	$tpl->assign("url",'&'.implode('&',convert($_GET)));
	}
	$tpl->assign("de",$de);
	$tpl->assign("countpro",$totalRows);
	$tpl->assign("config",$config);
	$tpl->display("product.htm");
?>
